package com.ybwh.springboot3.controller.mock;

import com.ybwh.springboot3.controller.mock.anno.Mocker;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.InvocationTargetException;

/**
 * <p>
 * 用mock替换对应的bean
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2025/8/14 10:42
 */
public class MockHandlerBeanPostProccess implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Mocker mk = bean.getClass().getAnnotation(Mocker.class);
        if (null == mk) {
            return bean;
        }

        try {
            return mk.type().getDeclaredConstructor().newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }


    }
}
